package LogisticsSystem.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import LogisticsSystem.mapper.CustomerMapper;
import LogisticsSystem.mapper.OrdersMapper;
import LogisticsSystem.mapper.OrderdetailMapper;
import LogisticsSystem.mapper.ReceiverMapper;
import LogisticsSystem.po.Customer;
import LogisticsSystem.po.Orders;
import LogisticsSystem.po.OrdersExample;
import LogisticsSystem.po.Orderdetail;
import LogisticsSystem.po.Receiver;
import LogisticsSystem.po.ReceiverExample;
import LogisticsSystem.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private ReceiverMapper receiverMapper;
	@Autowired
	private OrdersMapper orderMapper;
	@Autowired
	private OrderdetailMapper orderdetailMapper;

	/**
	 * 传入一个Customer对象，并判断其在数据库中是否存在，存在则更新数据库中的该对象,
	 */
	public Boolean updateCustomer(Customer customer) throws Exception {
		if (customer != null & customerMapper.selectByPrimaryKey(customer.getcId()) != null) {
			if (customerMapper.updateByPrimaryKey(customer) > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据id返回一个对应customer对象
	 */
	public Customer selectCustomer(Integer id) throws Exception {
		if (id != null) {
			return customerMapper.selectByPrimaryKey(id);
		}
		return null;
	}

	/**
	 * 根据顾客id返回该顾客的所有联系人
	 */
	public List<Receiver> showReceiver(Integer id) {

		ReceiverExample receiverExample = new ReceiverExample();
		LogisticsSystem.po.ReceiverExample.Criteria createCriteria = receiverExample.createCriteria();
		createCriteria.andCIdEqualTo(id);
		List<Receiver> receiverslist = receiverMapper.selectByExample(receiverExample);
		return receiverslist;

	}

	/**
	 * 根据顾客id返回该顾客的所有订单
	 */
	public List<Orders> showOrder(Integer id) {
		OrdersExample orderExample = new OrdersExample();
		LogisticsSystem.po.OrdersExample.Criteria createCriteria = orderExample.createCriteria();
		createCriteria.andCIdEqualTo(id);
		List<Orders> orderlist = orderMapper.selectByExample(orderExample);
		return orderlist;
	}

	/**
	 * 根据传入的联系人向数据库中新增
	 */
	public Boolean insertReciver(Receiver receiver) throws Exception {
		if (receiver != null) {
			if (receiverMapper.insertSelective(receiver) > 0) {// 插入语句有异议
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据传入联系人的ID在数据库中删除该联系人
	 */
	public Boolean deleteReciver(Integer id) throws Exception {
		if (id != null) {
			if (receiverMapper.deleteByPrimaryKey(id) > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据传入的order在数据库中新增订单,包括订单的商品详情
	 */
	public Boolean insertOrder(Orders order, Orderdetail orderdetail) throws Exception {
		if (order != null && orderdetail != null) {
			order.setoId(orderMapper.selectMaxOrder()+1);
			orderdetail.setoId(order.getoId());
			if (orderMapper.insert(order) > 0 && orderdetailMapper.insert(orderdetail) > 0) {
				return true;
			}
		}
		return false;
	}

	@Override
	public Boolean updateReciver(Receiver receiver) throws Exception {
		if (receiver != null & receiverMapper.selectByPrimaryKey(receiver.getrId())!=null) {
			if (receiverMapper.updateByPrimaryKey(receiver)>0) {
				return true;
			}
		}
		return false;
	}
}
